import $http from '../../axios'
import Vue from 'vue'
import {Toast} from 'mint-ui'

export default {
  namespaced: true,
  state:{
    navData: [],
    homeData: {
      homeBanner: '',
      homeAd:'',
      homeHot: '',
      homeNew: ''
    },
    hotSaleData:'',
    productTotal:{},
    singleProductDetailTotal:{},
    activityPic:[]
  },
  mutations:{
    getHomeData(state,{homeBanner,homeAd,homeHot,homeNew}){
      state.homeData.homeBanner = homeBanner;
      state.homeData.homeAd = homeAd;
      state.homeData.homeHot = homeHot;
      state.homeData.homeNew = homeNew;
    },
    getNavData(state,data){
      state.navData = data;
    },
    getHotSale(state,data){
      state.hotSaleData = data;
    },
    addProductsByType(state,{type,data}){
      Vue.set(state.productTotal,type,data);
    },
    addSingleDetail(state,{code,data}){
      code = code.split('-').join('_');
      Vue.set(state.singleProductDetailTotal,code,data);
    },
    addActivityPic(state,{data,index}){
      Vue.set(state.activityPic,parseInt(index),data);
    }
  },
  actions:{
    getHomeData(context){
      var homeData ={};
      $http.get('/public/getMachineMenu',null,{showLoading: false}).then(res => {
        if(res.data.status == 1){
          context.commit('getNavData',res.data.data);
        }
      })
      
      $http.get('public/getPhoneGoods',{type: 1}).then(res => {
        if(res.data.status == 1){
          let data = res.data.mapData;
          homeData.homeBanner = data.header;
          homeData.homeAd = data.banner;
          homeData.homeHot = data.rm;
          homeData.homeNew = data.xp;
          context.commit('getHomeData',homeData);
        } 
      })
    },
    getHotSale(context){
      $http.get('public/getPhoneGoods',{type:2}).then(res => {
        if(res.data.status == 1) context.commit('getHotSale',res.data.mapData)
        else console.log(res.data.message);
      }).catch(err => {
        console.log(err)
      })
    },
    getProductsByType(context,type){
    $http.get('public/getPhoneGoodsByMType',{machineType:type}).then(res => {
      if(res.data.status == 1) context.commit('addProductsByType',{type:type,data:res.data.mapData})
      else console.log(res.data.message);
    })
    },
    getSingleDetail(context,code){
      var encodedCode = encodeURIComponent(code);
      $http.get('/public/getPhoneGoods',{type:4,code:encodedCode}).then(res => {
        if(res.data.status == 1) {
          context.commit('addSingleDetail',{code:code,data:res.data.mapData})
        } else{
          console.log(res.data.message)
        };
      })
    },
    getActivityPic(context,{code,index}){
      $http.get('public/getBannerByCode',{code}).then(res => {
        if(res.data.status == 1){
          context.commit('addActivityPic',{data:res.data.data[0].url,index});
        }else {
          Toast({
            message: res.data.message,
            dutation: 1000,
            iconClass: 'iconfont jgsc-fail'
          })
        }
      })
    }
  }
}

